package com.it.schoolhomeos.controller;

import com.it.schoolhomeos.dto.ApiResponse;
import com.it.schoolhomeos.dto.ExamGradeDTO;
import com.it.schoolhomeos.dto.StudentExamGradeDTO;
import com.it.schoolhomeos.dto.StudentGradeDTO;
import com.it.schoolhomeos.entity.Grade;
import com.it.schoolhomeos.service.GradeService;
import com.it.schoolhomeos.service.ExamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 成绩控制器
 * 
 * 提供成绩相关的API接口
 */
@RestController
@RequestMapping("/api/grades")
@Tag(name = "成绩管理", description = "成绩相关的API接口")
public class GradeController {

    @Autowired
    private GradeService gradeService;
    
    @Autowired
    private ExamService examService;

    /**
     * 获取考试的某个科目的所有学生成绩
     */
    @GetMapping("/exam/{examId}/subject/{subject}")
    @Operation(summary = "获取考试科目成绩", description = "获取指定考试的指定科目的所有学生成绩")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<List<StudentGradeDTO>> getExamSubjectGrades(
            @PathVariable Long examId,
            @PathVariable String subject) {
        List<StudentGradeDTO> grades = gradeService.getExamSubjectGrades(examId, subject);
        return ApiResponse.success(grades);
    }
    
    /**
     * 获取学生在某次考试中的所有科目成绩
     */
    @GetMapping("/student/{studentId}/exam/{examId}")
    @Operation(summary = "获取学生考试成绩", description = "获取指定学生在指定考试中的所有科目成绩")
    public ApiResponse<Map<String, Double>> getStudentExamGrades(
            @PathVariable Long studentId,
            @PathVariable Long examId) {
        Map<String, Double> grades = gradeService.getStudentExamGrades(studentId, examId);
        return ApiResponse.success(grades);
    }
    
    /**
     * 录入学生成绩
     */
    @PostMapping("/enter")
    @Operation(summary = "录入学生成绩", description = "录入指定学生在指定考试中的指定科目成绩")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Grade> enterGrade(
            @Parameter(description = "考试ID") @RequestParam Long examId,
            @Parameter(description = "科目") @RequestParam String subject,
            @Parameter(description = "学生ID") @RequestParam Long studentId,
            @Parameter(description = "分数") @RequestParam Double score) {
        Grade grade = gradeService.enterGrade(examId, subject, studentId, score);
        return ApiResponse.success(grade);
    }
    
    /**
     * 批量录入学生成绩
     */
    @PostMapping("/batch-enter")
    @Operation(summary = "批量录入学生成绩", description = "批量录入指定考试中的指定科目的学生成绩")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<List<Grade>> batchEnterGrades(
            @Parameter(description = "考试ID") @RequestParam Long examId,
            @Parameter(description = "科目") @RequestParam String subject,
            @Parameter(description = "学生成绩映射（学生ID -> 分数）") @RequestBody Map<Long, Double> studentGrades) {
        List<Grade> grades = gradeService.batchEnterGrades(examId, subject, studentGrades);
        return ApiResponse.success(grades);
    }
    
    /**
     * 获取考试的所有学生成绩和排名
     */
    @GetMapping("/exam/{examId}/ranking")
    @Operation(summary = "获取考试排名", description = "获取指定考试的所有学生成绩和排名")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<List<StudentExamGradeDTO>> getExamGradesWithRanking(
            @PathVariable Long examId) {
        List<StudentExamGradeDTO> grades = gradeService.getExamGradesWithRanking(examId);
        return ApiResponse.success(grades);
    }
    
    /**
     * 获取学生的历史考试成绩
     */
    @GetMapping("/student/{studentId}/history")
    @Operation(summary = "获取学生历史成绩", description = "获取指定学生的历史考试成绩")
    public ApiResponse<List<ExamGradeDTO>> getStudentHistoryGrades(
            @PathVariable Long studentId) {
        List<ExamGradeDTO> grades = gradeService.getStudentHistoryGrades(studentId);
        return ApiResponse.success(grades);
    }

    /**
     * 获取班级成绩
     */
    @GetMapping("/class/{classId}")
    @PreAuthorize("hasRole('TEACHER')")
    @Operation(summary = "获取班级成绩", description = "获取指定班级的成绩记录，支持按科目和考试ID筛选")
    public ApiResponse<Page<Grade>> getClassGrades(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Parameter(description = "科目") @RequestParam(required = false) String subject,
            @Parameter(description = "考试ID") @RequestParam(required = false) Long examId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        return ApiResponse.success(gradeService.getClassGrades(classId, subject, examId, page, size));
    }

    /**
     * 获取成绩列表
     */
    @GetMapping
    @PreAuthorize("hasRole('TEACHER')")
    @Operation(summary = "获取成绩列表", description = "获取成绩记录列表，支持按学生、科目和考试ID筛选")
    public ApiResponse<Page<Grade>> getGrades(
            @Parameter(description = "学生ID") @RequestParam(required = false) Long studentId,
            @Parameter(description = "科目") @RequestParam(required = false) String subject,
            @Parameter(description = "考试ID") @RequestParam(required = false) Long examId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        return ApiResponse.success(gradeService.getGrades(studentId, subject, examId, page, size));
    }

    /**
     * 获取学生成绩
     */
    @GetMapping("/student/{studentId}")
    @Operation(summary = "获取学生成绩", description = "获取指定学生的成绩记录，支持按科目和考试ID筛选")
    @PreAuthorize("hasRole('PARENT')")
    public ApiResponse<List<Grade>> getStudentGrades(
            @Parameter(description = "学生ID") @PathVariable Long studentId,
            @Parameter(description = "科目") @RequestParam(required = false) String subject,
            @Parameter(description = "考试ID") @RequestParam(required = false) Long examId) {
        return ApiResponse.success(gradeService.getStudentGrades(studentId, subject, examId));
    }
} 